const {
  uniacid
} = require("../../host");
var t = getApp();
const {
  bindWxUser,
  getstatus,
  getZhuUser,
  getOpenid,
  getWxUser,
  getConfig,
  getUserStatus,
  delBindUser,
  createOrder,
  syncUser,
  xuqi
} = require('../common/api/user')
const {
  LogOut,
  getWxUserStatus
} = require('../common/api/send')
const api = require('../common/utils/request.js')


// haoduogeni/mine/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

    wxList: [],
    buyCradShow: false,
    isVip: true,
    rechargeShow: false,
    userInfo: {},
    bindUserShow: false,
    openid: '',
    wxuserInfo: {},
    page: 1,
    row: 10,
    uid: '',
    isBind: false,
    last_page: 1,
    syncUserShow: false,
    syncUserId: '',//同步用户id
    updateEndTimeShow: false,
    updateEndTimeId: '',//续期用户id
  },
  toFriendCicle(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/haoduogeni/friendCicle/index?id=' + id,
    })
  },
  loginOut(e) {
    console.log(e.currentTarget.dataset.wxid, 'eeeeeeeeeeeeeee')
    var wxid = e.currentTarget.dataset.wxid;
    var _this = this;
    wx.showModal({
      title: '提示', //提示的标题
      content: '确认退出登录么？', //提示的内容
      confirmText: '确定', //确认按钮的文字，最多 4 个字符
      cancelText: '取消', // 取消按钮的文字，最多 4 个字符
      success(res) {
        if (res.confirm) {
          // 用户点击确定后的操作 
          _this.loginoutsecond(wxid);
        } else {
          // 用户取消后的操作
        }
      }
    })


  },
  loginoutsecond(wxid) {
    var _this = this;
    api.post(LogOut, {
      wxid: wxid
    }).then(res => {
      //成功时回调函数
      console.log(res, 'getWxUser')
      if (res.errno == 0) {
        this.setData({
          page: 1,
          wxList: []
        })
        this.getWxList()
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    }).catch(err => {
      //失败时回调函数
      console.log(err)
    })
  },
  getWxUserInfo() {
    var _this = this,
      o = t.getNetAddresss("member.member.member-data");
    t._getNetWork({
      url: o,
      data: {
        v: 2,
        basic_info: 1
      },
      success: function (o) {
        var r = o.data;
        console.log(r, 'rrrrrrrrr')
        if (r.result == 1) {
          _this.setData({
            wxuserInfo: r.data.member
          })
        }
      }
    })
  },
  addAccount() {
    this.setData({
      buyCradShow: true,
      openid: this.data.userInfo.openid
    })
    // this.buyNow();

  },
  buyNow() {
    if (this.data.isLoading) {
      wx.showToast({
        title: '请勿重复点击',
        icon: 'none'
      })
      return
    }
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      isLoading: true
    })
    var _this = this;
    api.post(createOrder, {
      pt_id: t.globalData.ptId,
      uid: wx.getStorageSync('yz_uid'),
      type: 3, // 1充值积分2充值金币3购买套餐
      taocan_id: 33,
      pay_type: 2, //1微信支付2金币支付 （只有买套餐有金币支付）
      openid: this.data.openid
    }).then(res => {
      //成功时回调函数
      console.log(res.errno, 'getCodeList')
      if (res.errno == 0) {
        var payInfo = res.data.payInfo;

        wx.showToast({
          title: '购买成功',
          icon: 'none'
        })
        this.setData({
          isLoading: false
        })
        wx.hideLoading();
        this.setData({
          page: 1,
          wxList: []
        })
        _this.getWxList();
        // _this.triggerEvent("confirm");
        // _this.setData({
        //   show: false
        // });

      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
        this.setData({
          isLoading: false
        })
        wx.hideLoading();
      }
      // var payInfo = {
      //   appId: "wx3faa28fc6e94b17a",
      //   timestamp: "1754981691",
      //   nonceStr: "0ta2iapnv6w3kj51d2sqvmjxnxwe4q0e",
      //   package: "prepay_id=wx121454516049247774a73990d1abb40000",
      //   signType: "MD5",
      //   paySign: "8720FAA04BCC526F6C58E775B16DAF70",
      //   timestamp: "1754981691"
      // };
      // this.wechatPay(payInfo)
    }).catch(err => {
      //失败时回调函数
      console.log(err)
      this.setData({
        isLoading: false
      })
      wx.hideLoading();
    })
  },

  toBindWxUser(e) {
    var uid = this.data.uid;
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/haoduogeni/addAccount/index?uid=' + uid + '&id=' + id,
    })
  },
  toTongbuUser(e){
    var id = e.currentTarget.dataset.id;
    this.setData({
      syncUserShow: true,
      syncUserId: id
    })
  },
  toUpdateEndTime(e){
    var id = e.currentTarget.dataset.id;
    console.log(e,'ididdidididi')
    this.setData({
      updateEndTimeShow: true,
      updateEndTimeId: id
    })
  },
  confirmUpdateEndTime(data){
    if(this.data.isLoading){
      wx.showToast({
        title: '请勿重复点击',
        icon: 'none'
      })
      return
    }
    this.setData({
      isLoading: true
    })
    wx.showLoading({
      title: '加载中',
    })
    const day = data.detail;
    console.log(this.data.updateEndTimeId,'this.data.updateEndTimeId')
    api.post(xuqi, {
      pt_id: t.globalData.ptId,
      uid: wx.getStorageSync('yz_uid'),
      id: this.data.updateEndTimeId,
      day: day
    }).then(res => {
      //成功时回调函数
      console.log(res, 'getstatus')
      if (res.errno == 0) {
        this.setData({
          isLoading: false
        })
        wx.hideLoading();
        wx.showToast({
          title: '续期成功',
          icon: 'none'
        })
        this.setData({
          updateEndTimeShow: false,
          page: 1,
          wxList: [],
          updateEndTimeId: ''
        })
        this.getWxList();
      } else {
        this.setData({
          isLoading: false
        })
        wx.hideLoading();
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
        
      }
    }).catch(err => {
      this.setData({
        isLoading: false
      })
      wx.hideLoading();
      //失败时回调函数
      console.log(err)
    })
  },
  toBindWxUserReset(e) {
    console.log(e.currentTarget.dataset.item, 'toBindWxUserReset')
    var item = e.currentTarget.dataset.item;
    var obj = {
      id: item.id,
      uid: this.data.uid,
      DeviceId: item.wx_user.DeviceId,
      address: item.wx_user.address,
      login_type: item.wx_user.login_type,
      notes: item.wx_user.notes
    }
    wx.navigateTo({
      url: '/haoduogeni/addAccount/index?obj=' + JSON.stringify(obj),
    })
  },
  toDelBindUser(e) {
    console.log(e.currentTarget.dataset.id, 'eeeeeeeeeeeeeee')
    var id = e.currentTarget.dataset.id;
    var _this = this;
    wx.showModal({
      title: '提示', //提示的标题
      content: '确认清除绑定么？', //提示的内容
      confirmText: '确定', //确认按钮的文字，最多 4 个字符
      cancelText: '取消', // 取消按钮的文字，最多 4 个字符
      success(res) {
        if (res.confirm) {
          // 用户点击确定后的操作 
          _this.toDelBindUserSecond(id);
        } else {
          // 用户取消后的操作
        }
      }
    })
  },
  // 清除绑定
  toDelBindUserSecond(id) {
    api.post(delBindUser, {
      pt_id: t.globalData.ptId,
      id: id
    }).then(res => {
      //成功时回调函数
      console.log(res, 'getWxUser')
      if (res.errno == 0) {
        wx.showToast({
          title: '清除绑定成功',
          icon: 'none'
        })
        this.setData({
          page: 1,
          wxList: []
        })
        this.getWxList()
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    }).catch(err => {
      //失败时回调函数
      console.log(err)
    })
  },
  recharge() {
    if (!wx.getStorageSync('yz_uid')) {
      wx.showToast({
        title: '登录体验完整功能',
        icon: 'none',
        duration: 1000
      })
      return
    }
    this.setData({
      rechargeShow: true
    })
  },
  getWxList() {
    api.post(getWxUser, {
      pt_id: t.globalData.ptId,
      uid: wx.getStorageSync('yz_uid'),
      page: this.data.page,
      row: this.data.row
    }).then(res => {
      //成功时回调函数
      console.log(res, 'getWxUser')
      if (res.errno == 0) {
        var data = res.data.data;
        var last_page = res.data.last_page;
        this.setData({
          wxList: [...this.data.wxList, ...data],
          last_page: last_page
        })
      }
    }).catch(err => {
      //失败时回调函数
      console.log(err)
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  // onLoad(options) {



  // },
  // 检测登录状态
  getLoginStatus() {
    api.post(getWxUserStatus, {
      uid: this.data.uid
    }).then(res => {
      //成功时回调函数
      console.log(res, 'getZhuUser')
      if (res.errno == 0) {
        this.setData({
          page: 1,
          wxList: []
        })
        this.getWxList()
      }
    }).catch(err => {
      //失败时回调函数
      console.log(err)
    })
  },
  getCoin() {
    api.post(getZhuUser, {
      pt_id: t.globalData.ptId,
      uid: wx.getStorageSync('yz_uid')
    }).then(res => {
      //成功时回调函数
      console.log(res, 'getZhuUser')
      if (res.errno == 0) {
        this.setData({
          userInfo: res.data
        })
      }
    }).catch(err => {
      //失败时回调函数
      console.log(err)
    })
  },
  getUserInfo() {
    api.post(getZhuUser, {
      pt_id: t.globalData.ptId,
      uid: wx.getStorageSync('yz_uid')
    }).then(res => {
      //成功时回调函数
      console.log(res, 'getZhuUser')
      if (res.errno == 0) {
        this.setData({
          userInfo: res.data
        })
        if (!res.data.openid) {
          this.setData({
            isBind: false
          })
          this.getWxLogin()
        } else {
          this.setData({
            isBind: true
          })

        }
      }
    }).catch(err => {
      //失败时回调函数
      console.log(err)
    })
  },
  //同步会员
  toSyncUser(data){
    if(this.data.isLoading){
      wx.showToast({
        title: '请勿重复点击',
        icon: 'none'
      })
      return
    }
    this.setData({
      isLoading: true
    })
    wx.showLoading({
      title: '加载中',
    })
    const mobile = data.detail;
    api.post(syncUser, {
      pt_id: t.globalData.ptId,
      id: this.data.syncUserId,
      tel: mobile
    }).then(res => {
      //成功时回调函数
      console.log(res, 'getstatus')
      if (res.errno == 0) {
        this.setData({
          isLoading: false
        })
        wx.hideLoading();
        wx.showToast({
          title: '同步成功',
          icon: 'none'
        })
        this.setData({
          syncUserShow: false,
          page: 1,
          wxList: [],
          syncUserId: ''
        })
        this.getWxList();
      } else {
        this.setData({
          isLoading: false
        })
        wx.hideLoading();
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
        
      }
    }).catch(err => {
      this.setData({
        isLoading: false
      })
      wx.hideLoading();
      //失败时回调函数
      console.log(err)
    })
  },
  toBindUser(data) {
    console.log(data.detail, 'datatata')
    const mobile = data.detail;
    api.post(bindWxUser, {
      pt_id: t.globalData.ptId,
      uid: wx.getStorageSync('yz_uid'),
      tel: mobile
    }).then(res => {
      //成功时回调函数
      console.log(res, 'getstatus')
      if (res.errno == 0) {
        this.setData({
          userInfo: res.data,
          bindUserShow: false
        })
        this.getWxLogin();
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    }).catch(err => {
      //失败时回调函数
      console.log(err)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  toLogin() {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onLoad() {
    if (!wx.getStorageSync('yz_uid')) {
      wx.showToast({
        title: '登录体验完整功能',
        icon: 'none',
        duration: 1000
      })
      return
      // setTimeout(()=>{
      //   wx.navigateTo({
      //     url: '/pages/login/login',
      //   })
      // },1000)

    }
    this.getWxUserInfo();
    this.setData({
      page: 1,
      wxList: []
    })
    this.getWxList();


    api.post(getConfig, {
      pt_id: t.globalData.ptId
    }).then(res => {
      //成功时回调函数
      console.log(res, 'getConfig')
      var data = res.data;
      if (res.errno == 0) {
        if (data.type == 2) {
          //用户版

          this.getStatusUser(data.z_uid);
        } else {
          this.getStatus();
        }
      } else {

      }
    }).catch(err => {
      //失败时回调函数
      console.log(err)
    })

  },
  getStatusUser(uid) {
    api.post(getUserStatus, {
      pt_id: t.globalData.ptId,
      uid: wx.getStorageSync('yz_uid')
    }).then(res => {
      //成功时回调函数
      console.log(res, 'getstatus')
      if (res.errno == 0) {
        this.getUserInfo();
        this.setData({
          uid: uid
        });
        this.getLoginStatus()
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
        // this.setData({
        //   bindUserShow: true
        // })
      }
    }).catch(err => {
      //失败时回调函数
      console.log(err)
    })
  },
  getStatus() {
    api.post(getstatus, {
      pt_id: t.globalData.ptId,
      uid: wx.getStorageSync('yz_uid')
    }).then(res => {
      //成功时回调函数
      console.log(res, 'getstatus')
      if (res.errno == 0) {
        this.getUserInfo();
        this.setData({
          uid: res.data.uid
        });
        this.getLoginStatus();
      } else {
        this.setData({
          bindUserShow: true
        })
      }
    }).catch(err => {
      //失败时回调函数
      console.log(err)
    })
  },
  getWxLogin() {
    var _this = this;
    wx.login({
      success: function (a) {
        _this.getOpenIdNew(a.code)
      }
    })
  },
  confirm() {
    this.setData({
      page: 1,
      wxList: []
    })
    this.getWxList();
    this.getCoin();
  },
  confirmRecharge() {
    // this.getUserInfo()
    this.getCoin();
  },
  getOpenIdNew(code) {
    var _this = this;
    api.post(getOpenid, {
      pt_id: t.globalData.ptId,
      code: code,
      uid: wx.getStorageSync('yz_uid')
    }).then(res => {
      if (res.errno == 0) {
        console.log(res, 'code')
        this.setData({
          isBind: true
        })
        this.setData({
          page: 1,
          wxList: []
        })
        this.getWxList();
        this.getCoin();
      }
    }).catch(err => {
      console.log(err)
    })
  },
  closeBindUserShow() {
    this.setData({
      bindUserShow: false
    })
  },
  createPlan() {
    if (!wx.getStorageSync('yz_uid')) {
      wx.showToast({
        title: '登录体验完整功能',
        icon: 'none'
      })
      return
    }

    wx.navigateTo({
      url: '/haoduogeni/createPlan/index?uid=' + this.data.uid,
    })
  },
  createAIPlan() {
    if (!wx.getStorageSync('yz_uid')) {
      wx.showToast({
        title: '登录体验完整功能',
        icon: 'none'
      })
      return
    }
    wx.navigateTo({
      url: '/haoduogeni/createAIPlan/index?uid=' + this.data.uid,
    })
  },
  toplanList() {
    if (!wx.getStorageSync('yz_uid')) {
      wx.showToast({
        title: '登录体验完整功能',
        icon: 'none'
      })
      return
    }
    wx.navigateTo({
      url: '/haoduogeni/planList/index?uid=' + this.data.uid,
    })
  },
  toMore() {
    wx.showToast({
      title: '开发中',
      icon: 'none'
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log('到底了')
    if (this.data.page < this.data.last_page) {
      this.data.page++;
      this.setData({
        page: this.data.page
      })
      this.getWxList()
    } else {
      wx.showToast({
        title: '已加载全部',
        icon: 'none'
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})